#pragma once
#include <MsHTML.h>
#include "MyDOMTree.h"

class CDOMTree : public CTreeCtrl
{
	DECLARE_DYNAMIC(CDOMTree)
public:
	CDOMTree(){};
	virtual ~CDOMTree(){};
	void UpdateDOM(IHTMLDocument2* pDoc);
protected:
	bool PrepareDOMTree( void);
	HTREEITEM InsertDOMNode( DOMNode* pNode, HTREEITEM hparent);	
	// ExpandItem method wih FOREACHITEM signature so it can be parameter to DoForItemAndChildren
	void ExpandItem( HTREEITEM hItem) 
	{
		Expand(hItem,TVE_EXPAND);
	}
	void ExpandAll();
	void CollapseAll();
	// type of the CDOMTree method that will be called recursively for each node and all of its children
	typedef void (CDOMTree::*FOREACHITEM)(HTREEITEM);
	// flags for calling method on parent node and all of its children
	enum
	{
		// call method on the parent node first, then on its children
		// othwerwise, method is called on children first
		DOPARENTFIRST = 1,
		// don't call a method on topomost parent
		SKIPTOPPARENT = 2
	};
	// recursive procedure that will call fp for the item and for all of its children
	void DoForItemAndChildren( HTREEITEM hParent, FOREACHITEM fp, unsigned int flags = 0);
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnTvnDeleteitem(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnDestroy();
	afx_msg void OnExpandAll();
	afx_msg void OnCollapseAll();
private:
	CComPtr<IHTMLDocument2> m_spDoc2;
	MyDOMTree m_MyDOMTree;
public:
	afx_msg void OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
};

#define WM_DOMNODE_INFO WM_USER+101